package org.authsum.stitches.wicket;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.validation.validator.StringValidator;
import org.authsum.stitches.external.ContentDefHolder;
import org.authsum.stitches.external.ContentDefRequest;
import org.authsum.stitches.external.ContentDefResponse;
import org.authsum.stitches.wicket.contentdef.ContentDefsPanel;

import  org.authsum.stitches.wicket.common.SearchTips;

import java.util.*;

/*
 * this is a panel that would serve as a base for the landing
 * pages for each type
 * 
 * This panel will provide a means to create a new item, search for items
 * and select and edit an item 
 */
public abstract class BaseLandingPanel extends BasePanel {


	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Log logger = LogFactory.getLog(BaseLandingPanel.class);
	
	
	public BaseLandingPanel(String id) {
		super(id);
		this.setOutputMarkupId(true);
		backToSearchResults = createBackToSearchResultsLink();
		backToSearchResults.setOutputMarkupId(true);
		updateContainer.setOutputMarkupId(true);

		createSearchForm();
		
		initTable(new ArrayList());
		
		//let's set the results table to be invisible initially
		resultsTable.setVisible(false);
		
		add(updateContainer);
		feedbackPanel.setOutputMarkupId(true);
		add(feedbackPanel);
		addLink =createAddLink();
		
		add(addLink);
		
		
		
	}
	
	public void editDetailObject() {
		// getCenterDefaultPanel().setVisible(false);
		System.out.println("editDetailObject");
		resultsTable.setVisible(false);
		updateContainer.addOrReplace(detailPanel);

		addOrReplace(updateContainer);

	}
	
	
	protected void postInit() {
		detailPanel.setVisible(false);
		updateContainer.addOrReplace(detailPanel);
		
		
	}
	//I made SearchTips a generic panel so that subclasses can provide their own implementation
	protected Panel searchTips = new SearchTips("searchTips");
	
	protected Link backToSearchResults = null;
	
	protected Panel  resultsTable = null;
	protected WebMarkupContainer updateContainer = new WebMarkupContainer("updateContainer");
	protected Panel detailPanel = null;
	protected Form searchForm = null;
	protected Panel centerDefaultPanel = new Panel("centerDefaultPanel");
	
	protected Link createAddLink() {
		return (new Link("addLink") {
			private static final long serialVersionUID = 1L;

			/**
			 * Go to the Edit page, passing this page and the id of the Contact
			 * involved.
			 */
			@Override
			public void onClick() {
				createDetailObject();
				//we need to hide searchtips
				searchTips.setVisible(false);
				
			}
		});
	}
	
	protected Link addLink = null;
	
	
	protected abstract void initTable(List docs);

	public abstract void createDetailObject();
	
	
	protected void createSearchForm() {
		SearchParms searchParms = new SearchParms();

		searchForm = new Form("searchForm",
				new CompoundPropertyModel(searchParms));
		add(searchForm);




		searchForm.add(new TextField("keywords").add(StringValidator
				.maximumLength(128)));



		AjaxButton search = new AjaxButton("search", searchForm) {

			private static final long serialVersionUID = 1L;
			
			SearchParms searchParms = (SearchParms) searchForm
			.getModel().getObject();

			@Override
			protected void onError(AjaxRequestTarget target, Form form) {
				logger.debug("ajax search onError");
			}

			protected void onSubmit(AjaxRequestTarget target, Form form) {
				if (!validateSearch()) {
					target.addComponent(feedbackPanel);
					return;
				}
				performSearch(target,form);

			}
			
			private boolean validateSearch() {
				List<String> errors = searchParms.getValidity();
				if (!errors.isEmpty()) {

					// this.feedbackPanel.
					// this.error(message)

					Iterator<String> iterator = errors.iterator();
					while (iterator.hasNext()) {
						error(iterator.next());
					}
					return false;
				}
				return true;

			}
		};
		
		//we need this to be true so that the model is updated!
		search.setDefaultFormProcessing(true);

		searchForm.add(search);
		//PCR, we do not want to have this 2x in a page
		//searchFeedbackPanel.setOutputMarkupId(true);

		//searchForm.add(searchFeedbackPanel);

	}
	
	
	public abstract void performSearch(AjaxRequestTarget target, Form form);
	
	private Link createBackToSearchResultsLink() {
		return (new Link("backToSearchResults") {
			private static final long serialVersionUID = 1L;

			/**
			 * Go to the Edit page, passing this page and the id of the Contact
			 * involved.
			 */
			@Override
			public void onClick() {
				backToSearchResults.setVisible(false);
				detailPanel.setVisible(false);
				resultsTable.setVisible(true);
			}
		});
	}
	
	
}
